package com.mikey.forkjoin;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

/**
 * @author :Mikey
 * @description :
 * @create :2022-09-22 16:07:00
 */
public class Test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        test1();
        test2();
        test3();
    }

    //普通程序员
    public static void test1() {
        long sum = 0;
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10_0000_0000l; i++) {
            sum += i;
        }
        long end = System.currentTimeMillis();
        System.out.println("sum:" + sum + "时间：" + (end - start));
    }

    //forkjoin
    public static void test2() throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask<Long> task = new Demo(0l, 10_0000_0000l);
        ForkJoinTask<Long> submit = forkJoinPool.submit(task);
        Long sum = submit.get();
        long end = System.currentTimeMillis();
        System.out.println("sum:" + sum + "时间：" + (end - start));
    }

    //Stream 并行流
    public static void test3() {
        long start = System.currentTimeMillis();
        long sum = LongStream.rangeClosed(0l, 10_0000_0000l).reduce(0, Long::sum);
        long end = System.currentTimeMillis();
        System.out.println("sum:" + sum + "时间：" + (end - start));
    }
}
